using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using System.Data.SqlClient;
using NCS.Library.Components.Security;
using NCS.Library.Components.Roles;
using NCS.Library.Utilities;

namespace NCS.Module.CMS
{
    public partial class UpdateScript : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.btnRun.Click +=new EventHandler(btnRun_Click);
            this.btnConvertPass.Click +=new EventHandler(btnConvertPass_Click);
            this.btnConvertPass.Enabled = false;
        }
        private void btnConvertPass_Click(object o, EventArgs e)
        {
            if (this.txtPass.Text == "hoahong")
            {
                SqlConnection conn = new SqlConnection(AppEnv.ConnectionString);
                SqlCommand cmd = new SqlCommand("Select UserID, Password from Portal_Users where UserType=2", conn);
                cmd.CommandType = CommandType.Text;

                DataTable tbl = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(tbl);

                conn.Open();
                int i = 0;
                foreach (DataRow dr in tbl.Rows)
                {
                    i++;
                    RoleController.AddUserToRole(Convert.ToInt32(dr["UserID"]), AppEnv.DEFAULT_ROLE, ConvertUtility.ToInt32(AppEnv.PortalId()));
                    cmd.CommandText = "Update Portal_Users set Password='" + SecurityMethod.MD5Encrypt(Convert.ToString(dr["Password"]).Trim()) + "', UserType=0 Where UserID=" + Convert.ToString(dr["UserID"]);
                    cmd.ExecuteNonQuery();
                    this.txtScript.Text += "Update thanh cong " + i + "\n";
                }

                if (conn.State != ConnectionState.Closed) conn.Close();
            }
        }
        private void btnRun_Click(object o, EventArgs e)
        {
            if (this.txtPass.Text == "hoahong")
            {
                SqlConnection conn = new SqlConnection(AppEnv.ConnectionString);
                SqlCommand cmd = new SqlCommand(this.txtScript.Text, conn);
                cmd.CommandType = CommandType.Text;

                try
                {
                    if (this.chkCheckSelect.Checked)
                    {
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        this.dgResult.DataSource = tbl;
                        this.dgResult.DataBind();
                    }
                    else
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }
                    this.txtScript.Text = "OK";
                }
                catch (Exception ex)
                {
                    this.txtScript.Text = ex.Message;
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        this.txtScript.Text += "\nClose OK";
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
